<?php

namespace app\common\model;

use think\Model;

class Order extends Model
{
    protected $name = 'order';
    
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
    
    protected $append = ['status_text'];
    
    public function getStatusList()
    {
        return [
            0 => '待支付',
            1 => '已支付',
            2 => '已发货',
            3 => '已完成',
            4 => '已取消',
            5 => '已退款'
        ];
    }
    
    public function getStatusTextAttr($value, $data)
    {
        $status = $this->getStatusList();
        return isset($status[$data['status']]) ? $status[$data['status']] : '';
    }

    public function goods()
    {
        return $this->belongsTo('Goods', 'goods_id')->field('id,title,cover_image');
    }

    /**
     * 关联订单商品
     */
    public function items()
    {
        return $this->hasMany('OrderItem');
    }
}